<template>
  <el-card>
    <el-row :gutter="20" class="header">
      <el-col :span="7">
        <el-input
          placeholder="请输入科室的名称"
          clearable
          v-model="queryForm.query"
        ></el-input>
      </el-col>
      <el-button type="primary" :icon="Search" @click="initGetUsers">{{
        $t(`table.search`)
      }}</el-button>
      <el-button type="primary" @click="handleDialogValue()"
        >添加科室</el-button
      >
    </el-row>
    <el-table :data="tableData1" style="width: 100%">
      <el-table-column
        :width="item.width"
        :prop="item.prop"
        :label="item.label"
        v-for="(item, index) in options"
        :key="index"
      >
        <template v-slot="{ row }" v-if="item.prop === 'create_time'">
          {{ $filters.filterTimes(row.create_time) }}
        </template>
        <template #default="{ row }" v-else-if="item.prop === 'action'">
          <el-button
            type="primary"
            size="small"
            :icon="Edit"
            @click="handleDialogValue(row)"
          ></el-button>
          <el-button
            type="danger"
            size="small"
            :icon="Delete"
            @click="delUser(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:currentPage="currentPage4"
      v-model:page-size="queryForm.pagesize"
      :page-sizes="[2, 4, 6, 8]"
      :small="small"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>
  <Dialog
    v-model="dialogVisible"
    :dialogTitle="dialogTitle"
    v-show="dialogVisible"
    @initUserList="initGetUsers"
    :dialogTableValue="dialogTableValue"
  />
</template>

<script setup>
import { ref } from 'vue'
import { Search, Edit, Delete } from '@element-plus/icons-vue'
import { getOffice, deleteOffice } from '@/api/office.js'
import { options } from './options'
import Dialog from './components/dialog.vue'
import { isNull } from '@/utils/filter'
import { ElMessage, ElMessageBox } from 'element-plus'
const queryForm = ref({
  query: '',
  pagenum: 1,
  pagesize: 2
})
const dialogTitle = ref('')
const dialogVisible = ref(false)
const dialogTableValue = ref({})

const total = ref(0)
const tableData = ref([])

const tableData1 = ref([])

const display = () => {
  tableData1.value = []
  const a = (queryForm.value.pagenum - 1) * queryForm.value.pagesize
  for (var i = 0; i < total.value; i++) {
    if (i >= a && i < a + queryForm.value.pagesize) {
      console.log(tableData.value[i])
      tableData1.value.push(tableData.value[i])
    }
  }
}

const initGetUsers = async () => {
  const res = await getOffice()
  tableData.value = res.data.datas
  total.value = res.data.total
  display()
}
initGetUsers()

const handleSizeChange = (pageSize) => {
  queryForm.value.pagenum = 1
  queryForm.value.pagesize = pageSize
  initGetUsers()
}

const handleCurrentChange = (pageNum) => {
  queryForm.value.pagenum = pageNum
  initGetUsers()
}

const handleDialogValue = (row) => {
  if (isNull(row)) {
    dialogTitle.value = '添加科室'
    dialogVisible.value = true
    dialogTableValue.value = {}
  } else {
    dialogTitle.value = '编辑科室'
    dialogVisible.value = true
    dialogTableValue.value = JSON.parse(JSON.stringify(row))
  }
}

const delUser = (row) => {
  ElMessageBox.confirm('确定要删除吗', 'Warning', {
    confirmButtonText: 'OK',
    cancelButtonText: 'Cancel',
    type: 'warning'
  })
    .then(async () => {
      await deleteOffice(row.oid)
      ElMessage({
        type: 'success',
        message: 'Delete completed'
      })
      initGetUsers()
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: 'Delete canceled'
      })
    })
}
</script>

<style lang="scss" scoped>
.header {
  padding-bottom: 16px;
  box-sizing: border-box;
}
</style>